import { useEffect, useState } from 'react';
import moment from 'moment';
import _ from 'lodash';
import _isString from 'lodash/isString';

function limitLen(str, len = 12) {
    let result = '';
    if (_isString(str)) {
        if (str.length > len) {
            result = str.substr(0, len) + '...';
        } else {
            result = str;
        }
    } else {
        result = str;
    }
    return result;
}

function useFilterList({ tableState, columns, refreshTable }) {
    const [filterList, setFilterList] = useState([]);
    useEffect(() => {
        if (_.isEmpty(columns)) return;
        const _filters = convertFilter(tableState.current.filters);
        setFilterList(_filters);
    }, [tableState.current.filters, columns])

    const findColumn = (columns = [], key, keyName = 'key') => {
        const len = columns.length;
        for (let i = 0; i < len; i++) {
            const _c = columns[i];
            if (_c.children && _c.children.length) {
                const res = findColumn(_c.children, key, keyName);
                if (res) return res;
            }
            else {
                if (_c[keyName] === key) return _c;
            }
        }
    }

    const convertFilter = (filterMap) => {
        //这里table里面传过来的filterMap字段顺序会变化，修改过的字段会放到最后一位
        const _filterList = _.cloneDeep(filterList);
        for (let key in filterMap) {
            const val = filterMap[key];
            let filterColumn = findColumn(columns, key, 'dataIndex');
            let label = filterColumn ? filterColumn.title : "无标题";
            //  if (val === undefined || val === null || !filterColumn) return; // 不加!filterColumn 在关联申请单时默认筛选项会出bug
            if (!filterColumn || !filterColumn.filterOption) continue;

            const index = _.findIndex(_filterList, { key });
            if (!val) {
                if (index !== -1) {
                    _filterList.splice(index, 1);
                }
                continue;
            }
            let text;
            if (filterColumn.filterOption.type === 'dateRange') {
                text = moment(val[0]).format('YYYY/MM/DD') + ' ~ ' + moment(val[1]).format('YYYY/MM/DD');
            } else if (filterColumn.filterOption.type === 'numberRange') {
                text = val
            } else if (filterColumn.filterOption.type === 'department') {
                if (Array.isArray(val) && val.length > 0) {
                    text = limitLen(val.map(item => item.name).join(','));
                }
            } else {
                let filterValue = _.isArray(val) ? val : [val];
                text = filterValue.map(item => item?.text).join(',');
            }
            const _filter = { key, label, text, value: val, column: filterColumn };
            if (index !== -1) {
                if (val) {
                    _filterList[index] = _filter;
                }
            } else if (val) {
                _filterList.push(_filter);
            }
        }
        return _filterList;
    }

    const removeFilter = (key) => {
        const filterMap = {
            ...tableState.current.filters,
            [key]: null
        };
        refreshTable({ filters: filterMap }, { action: 'filter' });
    }

    const clearFilter = () => {
        const filterMap = {};
        for (let t in tableState.current.filters) {
            filterMap[t] = null;
        }
        refreshTable({ filters: filterMap }, { action: 'filter' });
    }

    return {
        filterList,
        removeFilter,
        clearFilter
    }
}

export default useFilterList;